<?php
if (!defined("LEVEL") || !defined("OK")) { header("Location: ?home"); }
if (isset($url['m']) && isnum($url['m']) && $url['m'] > 0) { $mid = (int)$url['m']; }
else { $mid = 0; }

// ############ Apdorojomi duomenys kurie buvo pateikti is tam tikros redagavimo lenteles #####################
// ######### Slaptazodzio keitimas #############
if (isset($_POST['action']) && $_POST['action'] == 'pass_change') {	$old_pass = md5($_POST['old_pass']);	$sql = mysql_num_rows(mysql_query1("SELECT * FROM users WHERE nick='" . $_SESSION['username'] ."' AND pass='" . $old_pass ."'"));
	if ($sql != 0) {		$new_pass = md5($_POST['new_pass']);
		$new_pass2 = md5($_POST['new_pass2']);
		if ($new_pass == $new_pass2) {			mysql_query1("UPDATE users SET pass='" . $new_pass ."' WHERE nick='" . $_SESSION['username'] ."'");
			msg("Info","Slaptažodis sėkmingai pakeistas");
			//echo LEVEL;
			//echo $_SESSION['username'];
		}
		else {			klaida("Klaida","Nesutampa naujieji slaptažodžiai");
		}
	}
	else {		klaida("Klaida","Klaidingas senasis slaptažodis");
	}
	unset($old_pass,$sql,$new_pass,$new_pass2);
}
// ################# kontaktu keitimas ######################
if (isset($_POST['action']) && $_POST['action'] == 'contacts_change') {	$icq = input($_POST['icq']);
	$msn = input($_POST['msn']);
	$skype = input($_POST['skype']);
	$yahoo = input($_POST['yahoo']);
	$aim = input($_POST['aim']);
	$url = input($_POST['url']);
	$email = input($_POST['email']);
	mysql_query1("UPDATE users SET icq='" . $icq ."', msn='" . $msn ."', skype='" . $skype ."', yahoo='" . $yahoo ."', aim='" . $aim . "', url='" . $url ."', email='" . $email ."' WHERE nick='" . $_SESSION['username'] ."'");
	msg("Info","Kontaktiniai duomenys sėkmingai atnaujinti");
	unset($icq,$msn,$skype,$yahoo,$aim,$url,$email);
}
// ################ Salies bei miesto nustatymai #############
if (isset($_POST['action']) && $_POST['action'] == 'country_change') {	$miestas = input($_POST['miestas']);
	$salis = input($_POST['salis']);
	mysql_query1("UPDATE users SET salis='" . $salis ."', miestas='" . $miestas ."' WHERE nick='" . $_SESSION['username'] ."'");
	msg("Info","Vietovės duomenys atnaujinti sėkmingai");
}

// ################ Parodoma Avataro direktorijos BET nerodomi avatarai #############
if (isset($_POST['action']) && $_POST['action'] == 'avatar_change') {	$avatar_text = "<fieldset>
  			<legend>Direktorijos pasirinkimas:</legend>
  			<center>$vimg</center>
  			<form name=\"avatar_change\" action=\"\" method=\"post\">
  			<table border=0 width=100%>
  				<tr>
  					<td align='left' width='10%'>Kategorija:</td>
  					<td><select size=\"1\" name=\"dir\">";
  						$tdir = "./images/avatars";
  						$dir = opendir("./images/avatars");
						while ($dira = readdir($dir)) {
							if (is_dir($tdir."/".$dira) && $dira != "." && $dira != "..") {
								$avatar_text .= "<option value='" . $dira ."'>$dira</option>\n";
							}
						}
					$avatar_text .= "</select>
					</td>
				</tr>
			</table>
			<input type=\"submit\"  value=\"Pasirinkti\">
			<input type=\"hidden\" name=\"action\"  value=\"image_change\" />
			</form>
			</fieldset>
	";
}
// ################ Pagrindiniu nustatymu keitimas ###################
if (isset($_POST['action']) && $_POST['action'] == 'default_change') {	$vardas = input($_POST['vardas']);
	$pavarde = input($_POST['pavarde']);
	$metai = (int)$_POST['metai'];
	$menesis = (int)$_POST['menesis'];
	$diena = (int)$_POST['diena'];
	$parasas = input($_POST['parasas']);
	$gimimas = $metai . "-" . $menesis ."-" . $diena;
	mysql_query1("UPDATE users SET vardas='" . $vardas . "', pavarde='" . $pavarde ."', parasas='" . $parasas ."', gim_data='" . $gimimas ."' WHERE nick='" . $_SESSION['username'] ."'");
	msg("Info","Pagrindiniai duomenys sėkmingai pakeisti");
}
// ################ Siulomi punktai redagavimui MENIU ##########################
$text = "
         <table width=100% border=0>
         	<tr>
         		<td valign=\"top\" width=\"20%\">
    				<fieldset>
    					<legend>Pasirinkite ką norite redaguoti:</legend>
    					<table border=0 width=100%>
    						<tr>
    							<td><center><a href='?id,44;m,1'><img src='images/user/user-auth.png' border='0' alt='Keisti pass' /></a></center></td>
    							<td><center><a href='?id,44;m,2'><img src='images/user/user-contact.png' border='0' alt='Kontaktai' /></a></center></td>
    							<td><center><a href='?id,44;m,3'><img src='images/user/user-place.png' border='0' alt='Vietove' /></a></center></td>
    						</tr>
    						<tr>
    							<td><center><a href='?id,44;m,1'>Slaptažodžio keitimas</a></center></td>
    							<td><center><a href='?id,44;m,2'>Kontaktiniai duomenys</a></center></td>
    							<td><center><a href='?id,44;m,3'>Vietovės nustatymas</a></center></td>
    						</tr>
    						<tr>
    							<td><center><a href='?id,44;m,4'><img src='images/user/user-avatar.png' border='0' alt='Keisti kaukę' /></a></center></td>
    							<td><center><a href='?id,44;m,5'><img src='images/user/user-settings.png' border='0' alt='Nustatymai' /></a></center></td>
    							<td></td>
    						</tr>
    						<tr>
    							<td><center><a href='?id,44;m,4'>Avataro redagavimas</a></center></td>
    							<td><center><a href='?id,44;m,5'>Pagrindiai nustatymai</a></center></td>
    							<td></td>
    						</tr>
    					</table>
    				</fieldset>
				</td>
			</tr>
		</table>
";

// ######################### Jei pasirinktas vienas is pasiulytu MENIU ####################
if (isset($mid) && isnum($mid)) {	// Pakeisti slaptazodi
	if ($mid == 1) {
			$text .= "
				<fieldset>
					<legend>Slaptažodžio keitimas</legend>
					<form name='change_password' action='' method='post'>
					<table border=0 width=100%>
						<tr>
							<td align='right' width='20%'>Senas slaptažodis:</td>
							<td><input name=\"old_pass\" type=\"password\" value=\"\"></td>
						</tr>
						<tr>
							<td align='right'>Naujas slaptažodis:</td>
							<td><input name=\"new_pass\" type=\"password\" value=\"\"></td>
						</tr>
						<tr>
							<td align='right'>Pakartokite slaptažodį:</td>
							<td><input name=\"new_pass2\" type=\"password\" value=\"\"></td>
						</tr>
						<tr>
							<td colspan=2>
								<input type=\"submit\" value=\"Keisti\">
								<input type=\"hidden\" name=\"action\" value=\"pass_change\" />
							</td>
						</tr>
					</table>
					</form>
			";
	}
	// Pakeisti kontaktinius duomenis
	if ($mid == 2) {			$info = mysql_fetch_assoc(mysql_query1("SELECT * FROM users WHERE nick=" . escape($_SESSION['username']) .""));			$text .= "
				<fieldset>
					<legend>Kontaktiniai duomenys</legend>
					<form name='change_contacts' action='' method='post' onSubmit=\"return checkMail('change_contacts','email')\">
					<table border=0 width=100%>
						<tr>
							<td align='right' width='15%'>ICQ:</td>
							<td><input name=\"icq\" type=\"text\" value=" . input($info['icq']) ."></td>
						</tr>
						<tr>
							<td align='right'>MSN:</td>
							<td><input name=\"msn\" type=\"text\" value=" . input($info['msn']) ."></td>
						</tr>
						<tr>
							<td align='right'>Skype:</td>
							<td><input name=\"skype\" type=\"text\" value=" . input($info['skype']) ."></td>
						</tr>
						<tr>
							<td align='right'>Yahoo:</td>
							<td><input name=\"yahoo\" type=\"text\" value=" . input($info['yahoo']) ."></td>
						</tr>
						<tr>
							<td align='right'>AIM:</td>
							<td><input name=\"aim\" type=\"text\" value=" . input($info['aim']) ."></td>
						</tr>
						<tr>
							<td align='right'>Iterneto svetainė:</td>
							<td><input name=\"url\" type=\"text\" value=" . input($info['url']) ."></td>
						</tr>
						<tr>
							<td align='right'>El. Paštas:</td>
							<td><input name=\"email\" type=\"text\" value=" . input($info['email']) ."></td>
						</tr>
						<tr>
							<td colspan=2>
								<input type=\"submit\" value=\"Atnaujinti\">
								<input type=\"hidden\" name=\"action\" value=\"contacts_change\" />
							</td>
						</tr>
					</table>
					</form>
				</fieldset>
			";
			unset($info);
	}
	// Pakeisti sali, miesta
	if ($mid == 3) {		$info = mysql_fetch_assoc(mysql_query1("SELECT salis, miestas FROM users WHERE nick='" . $_SESSION['username'] ."'"));
		$text .= "
				<fieldset>
					<legend>Vietovės nustatymas</legend>
					<form name='change_country' action='' method='post'>
					<table border=0 width=100%>
						<tr>
							<td align='right' width='5%'>Šalis:</td>
							<td>
								<select size=\"1\" name=\"salis\">
		";
		$sql = mysql_query1("SELECT * FROM salis");
		while ($row = mysql_fetch_assoc($sql)) {
  			$text .= "<option value='" . $row['iso'] ."' ";
  			if ($row['iso'] == $info['salis']) { $text .= "selected"; }
  			$text .=  ">" . $row['printable_name'] . "</option>\n";
  		}
  		$text .= "		</select>
  					</td>
  				</tr>
  				<tr>
  					<td align='right'>Miestas:</td>
  					<td><input name=\"miestas\" type=\"text\" value=" . $info['miestas'] .">
  				</tr>
  				<tr>
					<td colspan=2>
						<input type=\"submit\" value=\"Atnaujinti\">
						<input type=\"hidden\" name=\"action\" value=\"country_change\" />
					</td>
				</tr>
  			</table>
  			</form>
  			</fieldset>
  		";
  		unset($info,$sql,$row);
  	}

  	// Avataro keitimas
  	if ($mid == 4) {  		$sql = mysql_fetch_assoc(mysql_query1("SELECT `avatar` FROM users WHERE `nick`='" . $_SESSION['username'] ."'"));
  		$vimg = "<img src='" . $sql['avatar'] ."'></img>";
  		$text .= "
  			<fieldset>
  			<legend>Paveiksliuko pasirinkimas:</legend>
  			<center>$vimg
  			<form name=\"avatar_change\" action=\"\" method=\"post\">
			<input type=\"submit\" value=\"Pasikeisti\">
			<input type=\"hidden\" name=\"action\"  value=\"avatar_change\" />
			</form>
			$avatar_text
			</center>
			</fieldset>
		";
	}
  	// Pagrindiniai nustatymai
  	if ($mid == 5) {  		$sql = mysql_fetch_assoc(mysql_query1("SELECT * FROM users WHERE nick='" . $_SESSION['username'] ."'"));
  		$data = explode("-",$sql['gim_data']);  		$text .= "
  			<fieldset>
  			<legend>Pagrindiniai nustatymai</legend>
  			<form name=\"pagr_nustatymai\" action=\"\" method=\"post\">
  			<table border=0 width=100%>
  				<tr>
  					<td align='right' width='15%'>Vardas:</td>
  					<td><input name=\"vardas\" type=\"text\" value=" . input($sql['vardas']) ."></td>
  				</tr>
  				<tr>
  					<td align='right'>Pavardė:</td>
  					<td><input name='pavarde' type=\"text\" value=" . input($sql['pavarde']) ."></td>
  				</tr>
  				<tr>
  					<td align='right'>Gimimo data:</td>
  					<td>
  					<select size=\"1\" name=\"diena\">";
  					$a = 1;
  					while ($a < 31) {  						$text .= "<option value=" . $a . " ";
  						if($a == $data[2]) { $text .= "selected"; }
  						$text .= " >$a</option>\n";
  						$a++;
  					}
  					unset($a);
  					$text .= "
  					</select>
  					<select size=\"1\" name=\"menesis\">
  						<option value=\"1\" "; if($data[1] == 1) { $text .= "selected"; } $text .=">Sausis</option>
  						<option value=\"2\" "; if($data[1] == 2) { $text .= "selected"; } $text .=">Vasaris</option>
  						<option value=\"3\" "; if($data[1] == 3) { $text .= "selected"; } $text .=">Kovas</option>
  						<option value=\"4\" "; if($data[1] == 4) { $text .= "selected"; } $text .=">Balandis</option>
  						<option value=\"5\" "; if($data[1] == 5) { $text .= "selected"; } $text .=">Gegužė</option>
  						<option value=\"6\" "; if($data[1] == 6) { $text .= "selected"; } $text .=">Birželis</option>
  						<option value=\"7\" "; if($data[1] == 7) { $text .= "selected"; } $text .=">Liepa</option>
  						<option value=\"8\" "; if($data[1] == 8) { $text .= "selected"; } $text .=">Rugpjūtis</option>
  						<option value=\"9\" "; if($data[1] == 9) { $text .= "selected"; } $text .=">Rugsėjis</option>
  						<option value=\"10\" "; if($data[1] == 10) { $text .= "selected"; } $text .=">Spalis</option>
  						<option value=\"11\" "; if($data[1] == 11) { $text .= "selected"; } $text .=">Lapkritis</option>
  						<option value=\"12\" "; if($data[1] == 12) { $text .= "selected"; } $text .=">Gruodis</option>
					</select>
					<select size=\"1\" name=\"metai\">";
					$a = date("Y") - 80;
					$viso = date("Y") - 10;
					while ($a < $viso) {						$text .= "<option value=" . $a ." ";
						if ($data[0] == $a) { $text .= "selected"; } $text .=">$a</option>\n";
						$a++;
					}
					unset($viso,$a);
					$text .="</select></td>
				</tr>
				<tr>
					<td valign='top' align='right'>Parašas:</td>
					<td><textarea name=\"parasas\" rows=5 cols=30 wrap=\"on\">" . input($sql['parasas']) ."</textarea></td>
				</tr>
			</table>
			<input type=\"submit\" value=\"Atnaujinti\">
			<input type=\"hidden\" name=\"action\" value=\"default_change\" />
			</form>
			</fieldset>";
	}
}
// ############## VARTOTOJO Informacija ##############
else {	include "puslapiai/view_user.php";
	$text .= "</td></tr></table>";
}
lentele("Vartotojo nustatymai",$text);
?>
<script language="JavaScript1.2">
function checkMail(form,email) {
	var x = document.forms[form].email.value;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) { return true; }
	else { alert('Prašome patikrinti ar teisingai įvedėte e-pašto adresą'); return false; }
}
</script>